﻿using AIStudio.Wpf.AvalonDockMVVM.ViewModels;
using AIStudio.Wpf.HomePage.Helpers;
using AIStudio.Wpf.HomePage.Models;
using AIStudio.Wpf.HomePage.ViewModels;
using Prism.Common;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AIStudio.Wpf.HomePage.Views
{
    /// <summary>
    /// MainTabView.xaml 的交互逻辑
    /// </summary>
    public partial class MainTabView : UserControl
    {
        public MainTabView()
        {
            InitializeComponent();

            //此方法会漏掉未加载的dll
            //var assemblies = System.AppDomain.CurrentDomain.GetAssemblies().Where(p => p.FullName.StartsWith("AIStudio.Wpf")).ToList();

            //List<Assembly> assemblies = new List<Assembly>();
            //var assembly = Assembly.GetExecutingAssembly();
            //assemblies.Add(assembly);

            //var arrReferencedAssmbNames = assembly.GetReferencedAssemblies();
            //foreach (AssemblyName assmblyName in arrReferencedAssmbNames.Where(p => p.FullName.StartsWith("AIStudio.Wpf")))
            //{
            //    assembly = Assembly.Load(assmblyName);
            //    assemblies.Add(assembly);
            //}

            //foreach (var assemb in assemblies)
            //{
            //    foreach (var type in assemb.GetTypes())
            //    {
            //        if (type.IsSubclassOf(typeof(DockWindowViewModel)))
            //        {
            //            try
            //            {
            //                var name = type.FullName.Replace(".ViewModels.", ";component/Resources/").Replace("Model", "Resource");
            //                //为代码生成器做准备，把新增资源独立出来，新建View本页面不需要更改，只需要加新增文件即可。
            //                string requestedStyle = "/" + name + ".xaml";
            //                ResourceDictionary resourceDictionary = new ResourceDictionary();
            //                resourceDictionary.Source = new Uri(@requestedStyle, UriKind.RelativeOrAbsolute);
            //                this.Resources.MergedDictionaries.Add(resourceDictionary);
            //            }
            //            catch { }

            //        }
            //    }
            //}

            foreach (var type in AssemblyHelper.DockWindowViewModelTypes)
            {
                try
                {
                    var name = type.FullName.Replace(".ViewModels.", ";component/Resources/Dock/").Replace("Model", "Resource");
                    //为代码生成器做准备，把新增资源独立出来，新建View本页面不需要更改，只需要加新增文件即可。
                    string requestedStyle = "/" + name + ".xaml";
                    ResourceDictionary resourceDictionary = new ResourceDictionary();
                    resourceDictionary.Source = new Uri(@requestedStyle, UriKind.RelativeOrAbsolute);
                    this.Resources.MergedDictionaries.Add(resourceDictionary);
                }
                catch { }
            }

            var viewModel = new MainTabViewModel();
            this.DataContext = viewModel;            
        }

      
    }
}
